#include "widget.h"
#include "ui_widget.h"
#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QFileDialog>
#include <QDebug>
#include <string>
#include <QTime>
#include <time.h>

struct stu_info
{
    QString name;
    int num;
};


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpSocket = new QTcpSocket(this);

    m_pCamera = new QCamera(this);
    m_pViewfinder = new QCameraViewfinder(this);
    m_pImageCapture = new QCameraImageCapture(m_pCamera);
    m_pViewfinder->setMaximumWidth(300);
    m_pViewfinder->setMaximumHeight(400);


    QPushButton* button1 = new QPushButton("照相");
    QPushButton* button2 = new QPushButton("退出");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_pViewfinder);
    mainLayout->addWidget(button1);
    mainLayout->addWidget(button2);

    connect(button1, SIGNAL(clicked()), m_pImageCapture, SLOT(capture()));
    connect(button2, SIGNAL(clicked()), this, SLOT(exitBtnResponded()));
    connect(m_pImageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(cameraImageCaptured(int,QImage)));
    m_pImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
    m_pCamera->setViewfinder(m_pViewfinder);
    m_pCamera->start();
}

void Widget::connected_Slot() {
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT());
    //connect(m_pImageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(cameraImageCaptured(int,QImage)));


}

void Widget::readyRead_Slot(){
    ui->carNum->appendPlainText(tcpSocket->readAll());
   //ui->carNum->appendPlainText("123456");
}

Widget::~Widget()
{
    delete       m_pCamera;
    delete   m_pViewfinder;
    delete m_pImageCapture;

    delete ui;
}

void Widget::on_ConnectBtn_clicked()
{

    tcpSocket->connectToHost(ui->IPEdit->text(), ui->PortEdit->text().toUInt());
//    connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected_Slot()));
    if(tcpSocket->waitForConnected(1000))
    {
         qDebug()<<"connect server success.";
         connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(my_tcp_read()));
    }
    else
    {
         qDebug()<<"connect server fail.";
    }

}

void Widget::exitBtnResponded()
{
    m_pCamera->stop();
    close();
}
void Widget::cameraImageCaptured(int, QImage image)
{
    QString savepath = QFileDialog::getSaveFileName(this,"Save Capture","Capture","Image png(*.png);;Image jpg(*.jpg);;Image bmp(*.bmp)");
    if(!savepath.isEmpty()){
        image.save(savepath);
    }
}

void Widget::my_tcp_read()
{

    char msg[16]={0};
    tcpSocket->read(msg,sizeof(msg));
    qDebug() << msg << endl;

    qDebug() << "msg14" << msg[14] << endl;

    if(msg[14] == '0') {
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString OUTTime =current_date_time.toString("yyyy.MM.dd hh:mm:ss ddd");
        ui->outTime->appendPlainText(OUTTime);
        qDebug() << "222" << endl;
    }
    else {
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString INTime =current_date_time.toString("yyyy.MM.dd hh:mm:ss ddd");
        ui->inTime->appendPlainText(INTime);
        qDebug() << "111" << endl;
    }

    char carNUM[10] = {"123456"};
    int i = 0;
    for( i = 0; i < 10; i++){
       carNUM[i] = msg[i];
    }
    carNUM[i] = 0;
   qDebug() << carNUM << endl;
    ui->carNum->appendPlainText(carNUM);
}

